import indexRouter from './index'


function loadNav() {
  //获得路由数据
  let tokenData = JSON.parse(sessionStorage.getItem('loginData'));

  if (!!!tokenData) {
    return false;
  }
  let navList = JSON.parse(sessionStorage.getItem('loginData')).navList;
  //处理动态路由
  let foo = (data) => {
    let nav = [];
    for (let i = 0; i < data.length; i++) {
      if (data[i].children.length > 0) {
        nav.push({
          path: !!data[i].menuUrl ? '/' + data[i].menuUrl : '',
          name: !!data[i].menuUrl ? data[i].menuUrl : data[i].id,
          meta: {
            title: data[i].name,
            keepAlive: false,
            icon: data[i].menuIcon,
            hideInBread: data[i].menuHide
          },
          component: (!!data[i].menuComponent && data[i].menuComponent.length > 0) ? wzImport(data[i].menuComponent) : wzImport('main/test'),
          children: foo(data[i].children)
        })

      } else {
        nav.push({
          path: !!data[i].menuUrl ? '/' + data[i].menuUrl : '',
          name: !!data[i].menuUrl ? data[i].menuUrl : data[i].id,
          meta: {
            title: data[i].name,
            keepAlive: false,
            icon: data[i].menuIcon,
          },
          component: wzImport(data[i].menuComponent),
        })
      }
    }
    return nav;
  };
  const wzImport = file => () => import('@/' + file + '.vue');

  //获得处理后的动态路由
  let dataList = [];
  for (let i = 0; i < navList.length; i++) {
    if (!!navList[i].menus) {
      dataList = dataList.concat(foo(navList[i].menus))
    }
  }

  //动态路由处理
  indexRouter.addRoutes(dataList)

  let stateRoute = [
    {path: '*', redirect: "/404", meta: {hideInMenu: true}},
    {
      path: '/401', name: 'error_401', meta: {hideInMenu: true},
      component: () => import('@/components/error-page/401.vue')
    },
    {
      path: '/500', name: 'error_500', meta: {hideInMenu: true},
      component: () => import('@/components/error-page/500.vue')
    },
    {
      path: '/404', name: 'error_404', meta: {hideInMenu: true},
      component: () => import('@/components/error-page/404.vue')
    },

  ]
  //固定其它路由 追加 为了防止 刷新页面的时候 没有路由 直接跳转404  所以 统一追加
  indexRouter.addRoutes(stateRoute)

}

export default {loadNav}


export const navList = [
  // {path: '*', redirect: "/404", meta: {hideInMenu: true}},
  // {
  //   path: '/401', name: 'error_401', meta: {hideInMenu: true},
  //   component: () => import('@/components/error-page/401.vue')
  // },
  // {
  //   path: '/500', name: 'error_500', meta: {hideInMenu: true},
  //   component: () => import('@/components/error-page/500.vue')
  // },
  // {
  //   path: '/404', name: 'error_404', meta: {hideInMenu: true},
  //   component: () => import('@/components/error-page/404.vue')
  // },

  // TODO
  {
    path: '/', name: "_login",meta: {title: '登录'},component: () => import('@/components/main/login.vue')
  },
  // {
  //   path: '/', name: "_login",meta: {title: '登录'},component: () => import('@/components/main/login1.vue')
  // },
  {
    path: '', name: "_noticeList",component: () => import('@/components/main/main.vue'),meta: { keepAlive: false, title: '通知'},
    children: [
      {path: '/noticeList',name: "noticeList",meta: {title: '通知',},component: () => import('@/components/main/noticeList.vue')}
    ]
  },
  {
    path: '', name: "_dept",component: () => import('@/components/main/main.vue'),meta: { keepAlive: false, title: '首页'},
    children: [
      {path: '/dept',name: "dept",meta: {title: '首页',},component: () => import('@/components/main/dept.vue')}
    ]
  },
  {
    path: '', name: "_personalDetails",component: () => import('@/components/main/main.vue'),meta: { keepAlive: false, title: '个人信息'},
    children: [
      {path: '/personalDetails',name: "personalDetails",meta: {title: '个人信息',},component: () => import('@/components/main/personalDetails.vue')}
    ]
  },
  {
    path: '', name: "_changePassword",component: () => import('@/components/main/main.vue'),meta: { keepAlive: false, title: '修改密码'},
    children: [
      {path: '/changePassword',name: "changePassword",meta: {title: '修改密码',},component: () => import('@/components/main/changePassword.vue')}
    ]
  },
  {
    path: '/viewHelp', name: "viewHelp",component: () => import('@/components/main/viewHelp.vue'),meta: { keepAlive: false, title: '查看帮助'}
  },
  {
    path: '/vehiclePortrait', name: "vehiclePortrait",component: () => import('@/view/vehicle/vehiclePortrait.vue'),meta: { keepAlive: false, title: '车辆画像'}
  },
  {
    path: '/deptDetails', name: "deptDetails",component: () => import('@/view/deptDetails/deptDetails.vue'),meta: { keepAlive: false, title: '企业画像'}
  },


]
